package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import mappers.TeacherMapper;

import domains.Teacher;

public class AddTeacherGUI {
	private JFrame addTeacherFrame;
	private JPanel btnsPanel, panel;
	private JButton addBtn, cancelBtn;
	private JLabel teacherNameLbl, subjectNameLbl;
	private JTextField teacherNameTF, subjectNameTF;
	private GridLayout layout;
	
	public AddTeacherGUI(){
		
		addTeacherFrame = new JFrame();
		addTeacherFrame.setTitle("Add Teacher(s)");
		addTeacherFrame.setLayout(new BorderLayout());
		addTeacherFrame.setSize(400,300);
		
		layout = new GridLayout(0,2,1,0);
		
		panel = new JPanel();
		panel.setLayout(layout);
		layout.setVgap(0);
		
		teacherNameLbl = new JLabel("Teacher's name: ");
		teacherNameTF = new JTextField(15);
		
		subjectNameLbl = new JLabel("Subject: ");
		subjectNameTF = new JTextField(15);
			
		panel.add(teacherNameLbl);
		panel.add(teacherNameTF);
		panel.add(subjectNameLbl);
		panel.add(subjectNameTF);
		
		
		//------------
		
		btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		addBtn = new JButton("Add");
		addBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Teacher t = new Teacher(teacherNameTF.getText(), subjectNameTF.getText());
				TeacherMapper tMapper = new TeacherMapper();
				tMapper.addTeacher(t);
				
				int dialog = JOptionPane.showConfirmDialog(null, "Do you want to add one more?", 
						"WARNING", JOptionPane.YES_NO_OPTION);
				if(dialog == JOptionPane.YES_OPTION) {
					teacherNameTF.setText("");
					subjectNameTF.setText("");
				} else {
					addTeacherFrame.setVisible(false);
				}
			}	
		});
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addTeacherFrame.setVisible(false);
			}	
		});
		
		btnsPanel.add(addBtn);
		btnsPanel.add(cancelBtn);
		
		addTeacherFrame.add(panel, BorderLayout.CENTER);
		addTeacherFrame.add(btnsPanel, BorderLayout.SOUTH);
		addTeacherFrame.pack();
		addTeacherFrame.setVisible(true);
		
	}
}
